<template>
    <div>
        <el-dialog title="评论回复" :visible.sync="dialogVisible" width="700px" :close-on-click-modal="false">
            <div class="evaluate-item">
                <el-row :gutter="30">
                    <el-col :span="3">
                        <el-avatar :size="60" :src="source.avatar" lazy></el-avatar>
                    </el-col>
                    <el-col :span="20">
                        <div class="evaluate-item__title flex">
                            <span class="username">{{ source.username }}</span>
                        </div>
                        <div class="evaluate-item__content">
                            {{ source.content }}
                        </div>
                        <div class="evaluate-item__footer mt-10 flex space-between">
                            <div>
                                <span>评论时间：{{ source.create_time }}</span>
                            </div>
                            <div class="operate">
                                <!-- 暂时废弃 -->
                                <!-- <span :class="{ 'text-red': source.is_zan === 1 }" @click="dianzan(source)"><i
                                        class="el-icon-thumb"></i>点赞</span> -->
                            </div>
                        </div>
                    </el-col>
                </el-row>
            </div>

            <div class="divider">
                <el-divider></el-divider>
            </div>

            <h4 class="reply-total">{{ total }} 条回复</h4>
            <div class="reply-box">
                <el-scrollbar style="height:100%;width:100%" :wrapStyle="[{ 'overflow-x': 'hidden' }]" ref="scrollbar"
                    class="scrollbar">
                    <div class="evaluate-item mb-30" v-for="(item, index) in replyList" :key="index">
                        <el-row :gutter="30">
                            <el-col :span="3">
                                <el-avatar :size="60" :src="item.avatar" lazy></el-avatar>
                            </el-col>
                            <el-col :span="20">
                                <div class="evaluate-item__title flex align-items">
                                    <span class="username">{{ item.username }}</span>
                                    <div v-if="item.to_username">
                                        :回复
                                        <span class="to_username">@{{ item.to_username }}</span>
                                    </div>
                                </div>
                                <div class="evaluate-item__content">
                                    {{ item.content }}
                                </div>
                                <div class="evaluate-item__pics">
                                    <el-image :size="120" v-for="(item, index) in item.pics" :key="index"
                                        :src="item.pic" lazy></el-image>
                                </div>
                                <div class="evaluate-item__footer mt-10 flex space-between">
                                    <div>
                                        <span>发布时间：{{ item.create_time }}</span>
                                    </div>
                                    <div class="operate">
                                        <span @click="showReplyForm(item.id)"><i
                                                class="el-icon-chat-dot-square"></i>回复</span>

                                        <!-- 暂时废弃 -->
                                        <!-- <span :class="{ 'text-red': item.is_zan === 1 }" @click="dianzan(item)"><i
                                                class="el-icon-thumb"></i>点赞</span> -->
                                    </div>
                                </div>
                                <el-card class="replay-form mt-15" v-if="replyFormOpen[item.id]">
                                    <i class="el-icon-close" title="关闭" style="cursor: pointer;"
                                        @click="replyFormOpen[item.id] = false"></i>

                                    <form @submit.prevent="submitReply(item)" class="flex mt-10">
                                        <el-input v-model="replyForm.content"
                                            :placeholder="`回复@${ item.username }`"></el-input>
                                        <button type="submit" class="el-button el-button--danger"
                                            :class="{ 'is-disabled': replyForm.content == null }"
                                            :disabled="replyForm.content == null">回复</button>
                                    </form>
                                </el-card>
                                <div class="mt-10" v-if="item.replynum > 0">
                                    <el-button type="info" plain size="mini" @click="handleShowReplyAll(item)">
                                        查看全部 {{ item.replynum }} 条回复
                                        <i class="el-icon-arrow-right"></i>
                                    </el-button>
                                </div>
                            </el-col>
                        </el-row>
                    </div>
                    <div v-loading="loading" class="text-center">{{ loadStatus }}</div>
                </el-scrollbar>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { addSpuForumReply, getSpuForumReply, getAddSpuForumEvalZan } from '@/api/apis'
export default {
    name: "bbsDialog",
    data () {
        return {
            loading: true,
            dialogVisible: false,
            loadStatus: 'loadmore', //loadmore-加载前的状态，loading-加载中的状态，nomore-没有更多的状态
            source: {},
            replyList: [],
            isEmpty: false,
            total: 0, // 总条数
            params: {
                forum_id: '',
                page: 1, // 当前页
                pagesize: 10, // 每页条数
                lastPage: 1, // 最后一页
            },
            replyForm: {
                forum_id: '', // 评论id
                to_uid: '', // 回复给谁 一级评论不用传值
                reply_username: '', // 回复给谁 一级评论不用传值
                goods_id: this.$route.params.id,
                content: null,
                images: ''
            },
            replyFormOpen: {}
        }
    },
    watch: {
        source: {
            handler (newValue, oldValue) {
                this.params.forum_id = newValue.id;
                this.replyList = []
                this.params.page = 1
                this.params.lastPage = 1;
                this.getReplyList();
            },
            immediate: true, // 立即执行一次处理函数
            deep: true // 深度监听
        },
        dialogVisible: {
            handler (newValue, oldValue) {
                if (newValue) {
                    this.$nextTick(() => {
                        this.$refs.scrollbar.wrap.addEventListener("scroll", this.scrollContent, true)
                    })
                }
            },
            
        }
    },
    methods: {
        getReplyList(){
            let that = this;
            that.loading = true;
            that.loadStatus = '正在加载...';
            getSpuForumReply(this.params).then(res => {
                if (res.code === 1) {
                    that.replyList = [...that.replyList, ...res.data.data];
                    that.total = res.data.total;
                    that.params.pagesize = Number(res.data.per_page);
                    that.params.lastPage = res.data.last_page;
                    that.isEmpty = !that.replyList.length;
                    that.loadStatus = that.currentPage < res.data.last_page ? '加载更多' : '没有更多了';
                } else {
                    this.$message.error(res.msg)
                }
                this.loading = false;
            })
        },
        scrollContent (e) {
            // console.log(document.getElementsByClassName('is-scrolling-left')[0].scrollTop) 获取滚动条的高度
            let poor = e.target.scrollHeight - e.target.clientHeight
            if (poor == parseInt(e.target.scrollTop) || poor == Math.ceil(e.target.scrollTop) || poor == Math.floor(e.target.scrollTop)) {
                // 正在加载中，则不执行
                if (this.loading) true;
                // 判断总页数是否大于当前页，如果大于当前页，则加载下一页
                else if (this.params.page < this.params.lastPage) {
                    this.params.page += 1;
                    this.getReplyList()
                }
                
            }
        },
        showReplyForm (commentId) {
            const newReplyFormOpen = Object.keys(this.replyFormOpen).reduce((acc, key) => {
                acc[key] = false
                return acc
            }, {});
            newReplyFormOpen[commentId] = true;
            this.replyFormOpen = newReplyFormOpen;
        },
        submitReply (item) {
            // 提交回复
            this.replyForm.forum_id = item.forum_id
            this.replyForm.to_uid = item.uid
            addSpuForumReply(this.replyForm).then(res => {
                if (res.code === 1) {
                    this.$message.success(res.msg)
                    //this.$emit('refreshData')
                    //this.resetForm()
                    this.replyForm.content = null
                    this.replyFormOpen = {
                        ...this.replyFormOpen,
                        [item.id]: false
                    }
                } else {
                    this.$message.error(res.msg)
                }
            })
        },
        // 点赞
        dianzan (item) {
            if (item.is_zan) return this.$message.info('您已赞过了')
            getAddSpuForumEvalZan({
                fe_id: item.id,
                type: 1
            }).then(res => {
                if (res.code === 1) {
                    this.$message.success(res.msg)
                    item.is_zan = 1
                    this.$forceUpdate()
                } else {
                    this.$message.error(res.msg)
                }
            })
        },
    }
}
</script>

<style lang="less" scoped>
/deep/.el-dialog__header {
    border-bottom: 1px solid rgb(235, 236, 237);
}
.reply-box {
    height: calc(100vh - 600px);
    padding-top: 10px;
}
.evaluate-item {
    font-size: 16px;
    overflow: hidden;
    &__title {
        .username {
            color: #ff0000;            
        }
        .to_username {
            color: #ff0000;
        }
    }

    &__content {
        margin-top: 15px;
        font-size: 14px;
        color: #2E3033;

        p {
            line-height: 1.6;
            margin-bottom: 5px;
        }
    }


    &__footer {
        font-size: 14px;
        color: rgba(46, 48, 51, .6);

        span {
            &+span {
                margin-left: 30px;
            }

            i {
                margin-right: 5px;
            }
        }

        .operate {
            span {
                cursor: pointer;

                &:hover {
                    color: rgba(46, 48, 51, 1);
                }
            }
        }
    }

    .el-col-22 {
        padding-bottom: 30px;
        border-bottom: 1px solid rgba(46, 48, 51, .1)
    }
}

.divider {
    display: block;
    height: 20px;
    margin: 24px -20px 0;
    background-color: rgba(132, 145, 165, 0.1);
}

h4.reply-total {
    padding: 10px 0;
    border-bottom: 1px solid rgba(46, 48, 51, .1)
}
.replay-form {
    position: relative;
    .el-icon-close {
        position: absolute;
        top: 10px;
        right: 10px;
        transition: transform 0.5s ease;
        transform-origin: center;
        &:hover {            
            transform: rotate(90deg);
            color: #ff0000;
        }
    }
}
</style>